package cn.huobi.pet.service.impl;

import cn.huobi.basic.service.impl.BaseServiceImpl;
import cn.huobi.basic.util.BaiduAuditUtils;
import cn.huobi.pet.domain.Product;
import cn.huobi.pet.domain.ProductOnlineAuditLog;
import cn.huobi.pet.mapper.ProductDetailMapper;
import cn.huobi.pet.mapper.ProductMapper;
import cn.huobi.pet.service.IProductDetailService;
import cn.huobi.pet.service.IProductOnlineAuditLogService;
import cn.huobi.pet.service.IProductService;
import cn.huobi.product.domain.ProductOrderDetail;
import cn.huobi.product.service.IProductOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huobi
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private IProductDetailService productDetailService;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IProductOnlineAuditLogService productOnlineAuditLogService;
    @Autowired
    private IProductOrderDetailService productOrderDetailService;

    @Override
    public void add(Product product) {
        super.add(product);
        product.getDetail().setProductId(product.getId());
        productDetailService.add(product.getDetail());
    }

    @Override
    public void update(Product product) {
        super.update(product);
        product.getDetail().setProductId(product.getId());
        productDetailMapper.updateByProductId(product.getDetail());
    }

    @Override
    public void delete(Long id) {
        productDetailMapper.deleteByProductId(id);
        super.delete(id);
    }

    /**
     * 服务上线
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {
        Map<Long, Product> productMap = productMapper.findAll().stream().filter(product -> product.getState() == 0)
                .collect(Collectors.toMap(Product::getId, product -> product));
        Date date = new Date();
        for (Long id : ids) {
            Product product = productMap.get(id);
            StringBuilder stringBuilder = new StringBuilder();
            if (product != null){
                if (!BaiduAuditUtils.TextCensor(product.getName())){
                    stringBuilder.append("服务上架审核服务名称失败，服务名称为：" +product.getName()+";");
                }

                if (product.getResources().split(",").length>1){
                    if (!BaiduAuditUtils.ImgCensor(product.getResources().split(",")[0])){
                        stringBuilder.append("服务上架审核图片失败，服务名称为：" +product.getResources()+";");
                    }
                    if (!BaiduAuditUtils.ImgCensor(product.getResources().split(",")[1])){
                        stringBuilder.append("服务上架审核图片失败，服务名称为：" +product.getResources()+";");
                    }
                    if (!BaiduAuditUtils.ImgCensor(product.getResources().split(",")[2])){
                        stringBuilder.append("服务上架审核图片失败，服务名称为：" +product.getResources()+";");
                    }
                }else {
                    if (!BaiduAuditUtils.ImgCensor(product.getResources())){
                        stringBuilder.append("服务上架审核图片失败，服务名称为：" +product.getResources()+";");
                    }
                }

                if(stringBuilder.length() > 0){
                    ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                    productOnlineAuditLog.setAuditTime(date);
                    productOnlineAuditLog.setProductId(id);
                    productOnlineAuditLog.setNote(stringBuilder.toString());
                    productOnlineAuditLog.setState(0);
                    productOnlineAuditLogService.add(productOnlineAuditLog);
                    continue;
                }
                product.setState(1L);
                product.setOnsaletime(date);
                productMapper.update(product);
            }
        }
    }

    /**
     * 服务下线
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.findById(id);
            if (product.getState() == 1){
                productMapper.offsale(id);
            }
        }
    }

    /**
     * 服务详情界面
     * @param id
     * @return
     */
    @Override
    public Product getProductDetail(Long id) {
        return productMapper.getProductDetail(id);
    }

    @Override
    public void serviceOrder(Product product) {
        if (product.getId() !=null ){
            ProductOrderDetail productOrderDetail1 = new ProductOrderDetail();
            productOrderDetail1.setUserId(product.getUserId());
            productOrderDetail1.setProductId(product.getId());
            productOrderDetail1.setPrice(product.getSaleprice());
            productOrderDetail1.setShopId(product.getShopId());
            productOrderDetailService.add(productOrderDetail1);
        }
    }


}
